{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "548a165d",
   "metadata": {},
   "source": [
    "## 1. CandleRule中的相关配置\n",
    "\n",
    "与\"维护实时历史K线功能\"重要相关配置为：\n",
    "\n",
    "\n",
    "|参数|类型|默认值|说明|\n",
    "|:---|:---|:---|:---|\n",
    "|**SERVER_WEIGHT**|float, int|0.75|服务占用权重上限的比例 现货交易权重上限1200 合约交易权重上限2400|\n",
    "|**SYMBOLS**|str,list|'all'|收集的产品 all表示全部正在交易的产品|\n",
    "|**SYMBOLS_FILTER**|list|[]|过滤的产品|\n",
    "|**SYMBOL_CONTAINS**|str|''|产品名称中需要包含的内容|\n",
    "|**SYMBOL_ENDSWITH**|str|''|产品名称必须以何内容结尾|\n",
    "|**BAR**|str|binance_interface.binace_lite.BINANCE_DEFAULT_BAR|时间颗粒度|\n",
    "|**TIMEZONE**|str|binance_interface.binace_lite.BINANCE_TIMEZONE|时区|\n",
    "|**CANDLE_DIR**|str|binance_interface.binace_lite.BINANCE_CANDLE_DATE_BASE_DIR|以天为单位的数据存储路径|\n",
    "|**DOWNLOAD_TIME**|str, None|'00:10:00'|每日下载昨日历史K线数据的时刻 格式：%H:%M:%S None表示不下载（每日定时下载K线中使用）|\n",
    "|**LENGTH**|int|1440 * 2|candle_map 缓存K线数据长度（实时K线服务）|\n",
    "|**UPDATE_INTERVAL_SECONDS**|int|3|candle_map 更新时间间隔（秒）（实时K线服务）|\n",
    "|**CACHE_DELAY_SECONDS**|int, None|60 * 60|candle_map 保存缓存文件时间间隔（秒） None：不保存缓存（实时K线服务）|\n",
    "|**CACHE_DIR**|str|'./BINANCE_CACHE'|缓存数据路径（实时K线服务）|\n",
    "|**LOCAL_CANDLE_DAYS**|int|2|服务启动 需维护本地多少天的历史K线数据（实时K线服务）|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6eb57581",
   "metadata": {},
   "source": [
    "## 2. 重新定义规则"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "66ed1b43",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app.candle_server import CandleRule,CandleServer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fc56f2dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "class MyCandleRule(CandleRule):\n",
    "    # 收集的产品 all表示全部正在交易的产品\n",
    "    SYMBOLS = [\n",
    "        'BTCUSDT',\n",
    "        'ETHUSDT',\n",
    "    ]\n",
    "    # candle_map 缓存K线数据长度\n",
    "    LENGTH = 1440\n",
    "    # 时间颗粒度\n",
    "    BAR = '1m'\n",
    "    # 每日下载昨日历史K线数据的时刻 格式：%H:%M:%S None表示不下载\n",
    "    DOWNLOAD_TIME = None  # 如果你维护的实时K线长度不足以包含一整天，应该关闭DOWNLOAD_TIME功能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c24d185a",
   "metadata": {},
   "source": [
    "## 3. 启动实时K线维护"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a2bb7a91",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-01-26 01:24:21,864 [INFO] DOWNLOAD BTCUSDT    1m  24/01/2024\n",
      "2024-01-26 01:24:22,461 [INFO] DOWNLOAD ETHUSDT    1m  24/01/2024\n",
      "2024-01-26 01:24:22,463 [INFO] COMPLETE DOWNLOAD 24/01/2024 (ALL:2 SKIP:0 SUC:2 WARN:0 ERROR:0)\n",
      "2024-01-26 01:24:22,606 [INFO] COMPLETE DOWNLOAD 23/01/2024 (ALL:2 SKIP:2 SUC:0 WARN:0 ERROR:0)\n",
      "2024-01-26 01:24:24,614 [INFO] COMPLETE PREPARE candle_map\n",
      "2024-01-26 01:24:25,119 [INFO] DOWNLOAD CANDLE CACHE\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'BTCUSDT': array([[1.70611710e+12, 4.01380300e+04, 4.01498600e+04, ...,\n",
       "         3.90161400e+01, 1.56539079e+06, 0.00000000e+00],\n",
       "        [1.70611716e+12, 4.01231600e+04, 4.01270400e+04, ...,\n",
       "         2.72394000e+00, 1.09286470e+05, 0.00000000e+00],\n",
       "        [1.70611722e+12, 4.00949600e+04, 4.00949600e+04, ...,\n",
       "         1.44322000e+00, 5.78375603e+04, 0.00000000e+00],\n",
       "        ...,\n",
       "        [1.70620332e+12, 3.96406700e+04, 3.96580000e+04, ...,\n",
       "         1.22753300e+01, 4.86592058e+05, 0.00000000e+00],\n",
       "        [1.70620338e+12, 3.96580000e+04, 3.96660000e+04, ...,\n",
       "         1.30728800e+01, 5.18430454e+05, 0.00000000e+00],\n",
       "        [1.70620344e+12, 3.96659300e+04, 3.96659400e+04, ...,\n",
       "         1.49545000e+00, 5.93081059e+04, 0.00000000e+00]]),\n",
       " 'ETHUSDT': array([[1.70611710e+12, 2.23935000e+03, 2.24040000e+03, ...,\n",
       "         1.05016800e+02, 2.35143903e+05, 0.00000000e+00],\n",
       "        [1.70611716e+12, 2.23840000e+03, 2.23841000e+03, ...,\n",
       "         2.52323000e+01, 5.64626045e+04, 0.00000000e+00],\n",
       "        [1.70611722e+12, 2.23621000e+03, 2.23622000e+03, ...,\n",
       "         1.03699900e+02, 2.31610926e+05, 0.00000000e+00],\n",
       "        ...,\n",
       "        [1.70620332e+12, 2.18486000e+03, 2.18625000e+03, ...,\n",
       "         3.26805200e+02, 7.14110511e+05, 0.00000000e+00],\n",
       "        [1.70620338e+12, 2.18625000e+03, 2.18770000e+03, ...,\n",
       "         1.72418000e+02, 3.76989473e+05, 0.00000000e+00],\n",
       "        [1.70620344e+12, 2.18769000e+03, 2.18769000e+03, ...,\n",
       "         2.30900000e+01, 5.05026642e+04, 0.00000000e+00]])}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 币币交易：SPOT；U本位合约：UM；币本位合约：CM\n",
    "instType = 'SPOT'\n",
    "# 永续合约，默认规则\n",
    "candleServer = CandleServer(\n",
    "    instType=instType,\n",
    "    rule=MyCandleRule,\n",
    "    # 使用http和https代理，proxies={'http':'xxxxx','https:':'xxxxx'}，通requests中的proxies参数规则相同\n",
    "    proxies={},\n",
    "    # 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "    proxy_host=None,\n",
    ")\n",
    "# 启动K线维护服务（堵塞直到candle_map维护完成）\n",
    "candleServer.run_candle_map()\n",
    "# 被异步维护的candle_map字典\n",
    "candleServer.candle_map"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3e97245",
   "metadata": {},
   "source": [
    "## 4. 模拟实盘交易实时获取K线"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c27ca847",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:24:00 当前时间: 2024-01-26 01:24:25.634994\n",
      "[[1.70611710e+12 4.01380300e+04 4.01498600e+04 ... 3.90161400e+01\n",
      "  1.56539079e+06 0.00000000e+00]\n",
      " [1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " ...\n",
      " [1.70620332e+12 3.96406700e+04 3.96580000e+04 ... 1.22753300e+01\n",
      "  4.86592058e+05 0.00000000e+00]\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]]\n",
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:24:00 当前时间: 2024-01-26 01:24:35.635139\n",
      "[[1.70611710e+12 4.01380300e+04 4.01498600e+04 ... 3.90161400e+01\n",
      "  1.56539079e+06 0.00000000e+00]\n",
      " [1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " ...\n",
      " [1.70620332e+12 3.96406700e+04 3.96580000e+04 ... 1.22753300e+01\n",
      "  4.86592058e+05 0.00000000e+00]\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]]\n",
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:24:00 当前时间: 2024-01-26 01:24:45.638792\n",
      "[[1.70611710e+12 4.01380300e+04 4.01498600e+04 ... 3.90161400e+01\n",
      "  1.56539079e+06 0.00000000e+00]\n",
      " [1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " ...\n",
      " [1.70620332e+12 3.96406700e+04 3.96580000e+04 ... 1.22753300e+01\n",
      "  4.86592058e+05 0.00000000e+00]\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]]\n",
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:24:00 当前时间: 2024-01-26 01:24:55.643359\n",
      "[[1.70611710e+12 4.01380300e+04 4.01498600e+04 ... 3.90161400e+01\n",
      "  1.56539079e+06 0.00000000e+00]\n",
      " [1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " ...\n",
      " [1.70620332e+12 3.96406700e+04 3.96580000e+04 ... 1.22753300e+01\n",
      "  4.86592058e+05 0.00000000e+00]\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]]\n",
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:25:00 当前时间: 2024-01-26 01:25:05.645910\n",
      "[[1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " [1.70611728e+12 4.00644000e+04 4.00710800e+04 ... 3.49414000e+00\n",
      "  1.40000244e+05 0.00000000e+00]\n",
      " ...\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]\n",
      " [1.70620350e+12 3.96720100e+04 3.96795000e+04 ... 1.31595000e+00\n",
      "  5.22104965e+04 0.00000000e+00]]\n",
      "K线长度: 1440 K线的最新时间: 2024-01-26 01:25:00 当前时间: 2024-01-26 01:25:15.648133\n",
      "[[1.70611716e+12 4.01231600e+04 4.01270400e+04 ... 2.72394000e+00\n",
      "  1.09286470e+05 0.00000000e+00]\n",
      " [1.70611722e+12 4.00949600e+04 4.00949600e+04 ... 1.44322000e+00\n",
      "  5.78375603e+04 0.00000000e+00]\n",
      " [1.70611728e+12 4.00644000e+04 4.00710800e+04 ... 3.49414000e+00\n",
      "  1.40000244e+05 0.00000000e+00]\n",
      " ...\n",
      " [1.70620338e+12 3.96580000e+04 3.96660000e+04 ... 1.30728800e+01\n",
      "  5.18430454e+05 0.00000000e+00]\n",
      " [1.70620344e+12 3.96659300e+04 3.96659400e+04 ... 1.49545000e+00\n",
      "  5.93081059e+04 0.00000000e+00]\n",
      " [1.70620350e+12 3.96720100e+04 3.96795000e+04 ... 1.31595000e+00\n",
      "  5.22104965e+04 0.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import datetime\n",
    "\n",
    "# 打印实时更新的candle\n",
    "for i in range(6):\n",
    "    # 以安全的方式从candle_map中获取历史线，K线最新时间不能小于当前时间戳大于security_seconds秒\n",
    "    candle = candleServer.get_candle_security('BTCUSDT', security_seconds=60 * 3)\n",
    "    print(\n",
    "        'K线长度:',\n",
    "        candle.shape[0],\n",
    "        'K线的最新时间:',\n",
    "        datetime.datetime.fromtimestamp(int(candle[-1, 0] / 1000)),\n",
    "        '当前时间:',\n",
    "        datetime.datetime.now()\n",
    "    )\n",
    "    print(candle)  # 类型：np.ndarray\n",
    "    time.sleep(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e87a1cf3",
   "metadata": {},
   "source": [
    "## 5. 关闭服务"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8aa52a5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 关闭服务\n",
    "candleServer.close_run_candle_map()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
